package com.hmp.recruit.common.util.security;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import com.hmp.recruit.common.util.config.AppProp;

public class AESUtil {

	public static void main(String[] args) throws Exception {
		String str = "sys/user/validateEmail/";
		String key = AppProp.get("app.securityKey");
		String encrytStr;
		byte[] encrytByte;

		byte[] byteRe = enCrypt(str, key);

		// 加密过的二进制数组转化成16进制的字符串
		encrytStr = parseByte2HexStr(byteRe);
		System.out.println("加密后：" + encrytStr);

		// 加密过的16进制的字符串转化成二进制数组
		encrytByte = parseHexStr2Byte(encrytStr);
		System.out.println("解密后：" + deCrypt(encrytByte, key));
		
		String securityKey = AESUtil.parseByte2HexStr(AESUtil.enCrypt("903441294@qq.com" + "123456", AppProp.get("app.securityKey")));
		String password = AESUtil.parseByte2HexStr(AESUtil.enCrypt("123456", securityKey));
		System.out.println(securityKey);
		System.out.println(securityKey.length());
		System.out.println(password);
		System.out.println(password.length());
	}

	/**
	 * 加密函数
	 * 
	 * @param content
	 *            加密的内容
	 * @param strKey
	 *            密钥
	 * @return 返回二进制字符数组
	 * @throws Exception
	 */
	public static byte[] enCrypt(String content, String strKey) {
		KeyGenerator keygen = null;
		SecretKey desKey = null;
		Cipher c = null;
		byte[] cByte = null;
		String str = content;

		try {
			keygen = KeyGenerator.getInstance("AES");
			keygen.init(128, new SecureRandom(strKey.getBytes()));

			desKey = keygen.generateKey();
			c = Cipher.getInstance("AES");

			c.init(Cipher.ENCRYPT_MODE, desKey);

			cByte = c.doFinal(str.getBytes("UTF-8"));
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return cByte;
	}

	/**
	 * 解密函数
	 * 
	 * @param src  加密过的二进制字符数组
	 * @param strKey 密钥
	 * @return
	 */
	public static String deCrypt(byte[] src, String strKey) {
		KeyGenerator keygen;
		SecretKey desKey;
		Cipher c;
		byte[] cByte;

		try {
			keygen = KeyGenerator.getInstance("AES");
			keygen.init(128, new SecureRandom(strKey.getBytes()));

			desKey = keygen.generateKey();
			c = Cipher.getInstance("AES");

			c.init(Cipher.DECRYPT_MODE, desKey);

			cByte = c.doFinal(src);

			return new String(cByte, "UTF-8");
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 2进制转化成16进制
	 * 
	 * @param buf
	 * @return
	 */
	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 将16进制转换为二进制
	 * 
	 * @param hexStr
	 * @return
	 */
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

}